//
//  LBCalendarPickerView.m
//  jslj
//
//  Created by 杨庆人 on 2018/4/9.
//  Copyright © 2018年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCalendarPickerView.h"
#import "LBCalenderTitleView.h"
#import <LBCategory/LBCategory.h>


#define pop_H ([LBCalenderPopRoundView viewHight] + [UIApplication sharedApplication].statusBarFrame.size.height - 20)

@interface LBCalendarPickerView ()
@property (nonatomic,strong)LBCalenderPopRoundView *popRoundView;
@end

@implementation LBCalendarPickerView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    
    self.popRoundView = [[LBCalenderPopRoundView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, pop_H) type:self.type timeStampType:self.timeStampType format:self.format];
    
    [self.view addSubview:self.popRoundView];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.popRoundView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(13, 13)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.popRoundView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.popRoundView.layer.mask = maskLayer;
    
    __weak typeof(self)weakSelf = self;
    self.popRoundView.close = ^{
        [weakSelf dismiss];
    };
    
    self.popRoundView.select = ^(NSString *timeStamp, NSString *timeStr) {
        !weakSelf.select ? :weakSelf.select(timeStamp,timeStr);
        
        [weakSelf dismiss];
    };
    
    // Do any additional setup after loading the view.
}

+ (void)showWithFormat:(NSString *)format result:(LBCalendarSelectBlock)select {
    [self.class showByPickerType:LBCalendarPickerSelectDateNormal format:format result:select];
}


+ (void)showByPickerType:(LBCalendarPickerType)type format:(NSString *)format result:(LBCalendarSelectBlock)select {
    [self.class showByPickerType:type timeStampType:LBCalendarTimeStampTypeDayEnd format:format result:select];
}

+ (void)showByPickerType:(LBCalendarPickerType)type timeStampType:(LBCalendarTimeStampType)timeStampType format:(NSString *)format result:(LBCalendarSelectBlock)select {
    LBCalendarPickerView * vc = [[LBCalendarPickerView alloc] init];
    vc.providesPresentationContextTransitionStyle = YES;
    vc.definesPresentationContext = YES;
    vc.type = type;
    vc.select = select;
    vc.format = format;
    vc.timeStampType = timeStampType;
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [[UIApplication getCurrentViewController] presentViewController:vc animated:NO completion:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.popRoundView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height -pop_H, [[UIScreen mainScreen] bounds].size.width, pop_H);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

- (void)dismiss {
    __weak typeof(self)weakSelf = self;
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.popRoundView.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, pop_H);
    } completion:^(BOOL finished) {
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
}

@end

#define title_H 42.5
@interface LBCalenderPopRoundView ()
@property(nonatomic,strong)LBCalenderTitleView *titleView; //title
@property(nonatomic,strong)LBCalendarContentView *contentView;
@property (nonatomic,assign)LBCalendarPickerType type;
@property (nonatomic,assign)LBCalendarTimeStampType timeStampType;
@property (nonatomic,copy)NSString *format;

@end
@implementation LBCalenderPopRoundView

- (instancetype)initWithFrame:(CGRect)frame type:(LBCalendarPickerType)type timeStampType:(LBCalendarTimeStampType)timeStampType format:(NSString *)format {
    if (self = [super initWithFrame:frame]) {
        _type = type;
        _format = format;
        _timeStampType = timeStampType;
        [self setCalendarContentView];
    }
    return self;
}

- (void)setClose:(CloseBlock)close {
    _close = close;
    self.titleView.closeBlock = _close;
}

- (void)setCalendarContentView {
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.titleView];
    
    self.contentView = [[LBCalendarContentView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleView.frame), self.bounds.size.width , [LBCalendarContentView viewHight]) type:self.type format:self.format];
    
    self.contentView.timeStampType = self.timeStampType;
    
    [self.contentView setLastOrNextMonDayAnimated:NO];
    
    [self addSubview:self.contentView];
}

- (void)setSelect:(LBCalendarSelectBlock)select {
    _select = select;
    self.contentView.select = select;
}

- (LBCalenderTitleView *)titleView {
    if (!_titleView) {
        _titleView = [[LBCalenderTitleView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, title_H)];
    }
    return _titleView;
}

+ (CGFloat)viewHight {
    return title_H + [LBCalendarContentView viewHight];
}

@end

